package main

import (
	"demo2/entity"
	"fmt"
	"gitee.com/baizhige/mongong-go"
	"gitee.com/baizhige/mongong-go/db"
	"gitee.com/baizhige/mongong-go/processor"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"time"
)

func main() {
	// 演示数据库初始化
	err := db.InitDB("mongodb://root:cT93YrhLJt3nwDuX@8.134.198.135:6664/", "demo1")
	if err != nil {
		return
	}

	// 演示数据操作
	user := &entity.User{
		Name:       "demo2-user1",
		Age:        2,
		CreateTime: time.Now(),
	}
	result, err := mongong.Coll[entity.User]().InsertOne(user)
	if err != nil {
		fmt.Println(err)
		return
	}

	// *****演示使用插件*****
	// 插件启用前 插入不会加密数据
	mongong.Coll[entity.UserCard]().InsertOne(&entity.UserCard{
		UID:    result.InsertedID.(primitive.ObjectID),
		CardNo: "demo2-car001",
	})
	//插件启用
	processor.Register(processor.InsertType, processor.EncProcessor{}) //插件启用 只要实现了processor.EncData的接口的对象入库都会自动执行其EncodeObj方法
	mongong.Coll[entity.UserCard]().InsertOne(&entity.UserCard{
		UID:    result.InsertedID.(primitive.ObjectID),
		CardNo: "demo2-car002",
	})

	// 并且不影响其他数据入库
	mongong.Coll[entity.User]().InsertOne(&entity.User{
		Name:       "demo2-user2",
		Age:        2,
		CreateTime: time.Now(),
	})
}
